﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using DBSourceToolsLib;
using DBSourceToolsLib.Interfaces;
using DBSourceToolsLib.SysObjects;
using DBConnectionLib;

namespace DBSourceTools
{
    public partial class ServerExplorer : DockContent, DBSourceToolsLib.Interfaces.IDBExplorerSubject
    {
        List<DBSourceToolsLib.Interfaces.IDBExplorerObserver> m_ListObservers
            = new List<DBSourceToolsLib.Interfaces.IDBExplorerObserver>();

        Hashtable m_ServerHash = new Hashtable();

        public ServerExplorer()
        {
            InitializeComponent();
        }

        #region IDBExplorerSubject Members

        public void RegisterInterestInDbExplorer(IDBExplorerObserver observer)
        {
            this.m_ListObservers.Add(observer);
        }

        #endregion

        private void ServerExplorer_Load(object sender, EventArgs e)
        {
            try
            {

                if (UserSettings.Instance().DatabaseProject != null)
                {
                    foreach (DBSourceToolsLib.SourceDatabase oSource in UserSettings.Instance().DatabaseProject.SourceDatabases)
                    {
//                        AddDatabase(oSource.DataSourceName, oSource.DatabaseName, oSource.NickName);
                    }
                    // add target databases
                    foreach (DBSourceToolsLib.SourceDatabase oSource in UserSettings.Instance().DatabaseProject.SourceDatabases)
                    {
                        foreach (DBSourceToolsLib.TargetDatabase oTarget in oSource.TargetDatabases)
                        {
//                            AddDatabase(oTarget.DataSourceName, oTarget.DatabaseName, oTarget.NickName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                    new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(ex.ToString());
                oError.ShowDialog(this);
            }

        }

        private void AddDatabase(string strServer, string strDatabase, string strNick)
        {
            string strKey = strServer;
            if (strKey.CompareTo(".") == 0)
            {
                strKey = "(local)";
            }
            if (m_ServerHash.ContainsKey(strKey))
            {
                List<DBListItem> dbList = (List<DBListItem>)m_ServerHash[strKey];
                bool bFound = false;
                foreach (DBListItem oItem in dbList)
                {
                    if ((oItem.Server.CompareTo(strServer) == 0)
                        && (oItem.Database.CompareTo(strDatabase) == 0))
                    {
                        bFound = true;
                        break;
                    }
                }
                if (!bFound)
                {
                    DBListItem oNewItem = new DBListItem(strServer, strDatabase, strNick);
                    dbList.Add(oNewItem);
                    m_ServerHash[strKey] = dbList;

                }
            }
            else
            {
                List<DBListItem> dbList = new List<DBListItem>();
                DBListItem oNewItem = new DBListItem(strServer, strDatabase, strNick);
                dbList.Add(oNewItem);
                m_ServerHash.Add(strKey, dbList);
            }

        }

    }
}
